﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/*
    2012-2-17 David检查正常
 */
namespace Benefit.Service.Sys
{
    /// <summary>
    /// 账户表(Server中的虚拟账户)
    /// </summary>
    public class ServiceAccount : Benefit.Interface.Sys.IAccount
    {
        Benefit.DB.DBManager db = null;
        public ServiceAccount(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 从Server中发现新的账号并临时保存
        /// </summary>
        /// <param name="loginAccount">Server中的登录名</param>
        public void InsertIntoBenefitFromServer(string loginAccount)
        {
            using (DB.DBManager db = new DB.DBManager())
            {
                Models.Sys.Account account = new Models.Sys.Account();
                account.AccountName = loginAccount;
                account.IsDeleted = false;
                account.DeletedTime = DateTime.Now;
                db.Accounts.Add(account);
                db.SaveChanges();
                db.Dispose();
            }
        }
        /// <summary>
        /// 获取所有分析系统的账号
        /// </summary>
        /// <returns></returns>
        public List<Models.Sys.Account> GetList()
        {
            using (DB.DBManager db = new DB.DBManager())
            {
                var query = db.Accounts.ToList();
                db.Dispose();
                return query;
            }
        }
        /// <summary>
        /// 获取所有分析系统未被删除的账号
        /// </summary>
        /// <returns></returns>
        public List<Models.Sys.Account> GetUnDeleteList()
        {
            var query = db.Accounts.Where(a => a.IsDeleted == false).ToList();
            return query;
        }
        /// <summary>
        /// 根据账户名得到账户编号
        /// </summary>
        /// <param name="accountName"></param>
        /// <returns></returns>
        public int GetAccountId(string accountName)
        {
            using (DB.DBManager db = new DB.DBManager())
            {
                int id = -1;
                var query = from t in db.Accounts where t.AccountName.Equals(accountName) select t;
                if (query.Count() > 0)
                {
                    id = query.First().Id;
                }
                db.Dispose();
                return id;
            }
        }



        /// <summary>
        /// 找到某日同一个批次交易员的所有账号
        /// </summary>
        /// <param name="Batch"></param>
        /// <returns></returns>
        public List<Models.Sys.Account> GetBatchAccounts(int batch, int tradeHistoryId)
        {
            using (DB.DBManager db = new DB.DBManager())
            {
                Interface.Sys.IOperator iop = new Sys.ServiceOperator(db);
                List<Models.Sys.Operator> operators = iop.GetBatchOperators(batch);
                db.Dispose();
                return GetOperatorsAccount(operators, tradeHistoryId);
            }
        }

        /// <summary>
        /// 得到一些交易员的所有账号
        /// </summary>
        /// <param name="operators"></param>
        /// <returns></returns>
        public List<Models.Sys.Account> GetOperatorsAccount(List<Models.Sys.Operator> operators, int tradeHistoryId)
        {
            using (DB.DBManager db = new DB.DBManager())
            {
                Interface.Sys.ITradeHistory ith = new Sys.ServiceTradeHistory(db);
                DateTime _pdate = ith.GetDateTimeFromTradeHistoryId(tradeHistoryId);
                List<Models.Sys.Account> accounts = new List<Models.Sys.Account>();
                Interface.Sys.IDayOperatorAccount idoa = new Sys.ServiceDayOperatorAccount(db);
                foreach (Models.Sys.Operator ops in operators)
                {
         
                    List<Models.Sys.Account> accs = idoa.GetDayOperatorAccounts(_pdate, ops.Id);
                    foreach (Models.Sys.Account a in accs)
                    {
                        accounts.Add(a);
                    }
                }
                db.Dispose();
                return accounts;
            }
        }

        /// <summary>
        /// 判断账号在某个月中是否变换过所有者
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="accountid"></param>
        /// <returns></returns>
        public bool HasChangedOwnerInMonth(int year, int month, int accountid)
        {
            using (DB.DBManager db = new DB.DBManager())
            {
                bool hasChanged = false;
                for (int i = 1; i < 6; i++)
                {
                    hasChanged = HasChangedOwnerInWeek(year, month, i, accountid);
                    if (hasChanged == true)
                    {
                        break;
                    }
                }
                db.Dispose();
                return hasChanged;
            }
        }
        /// <summary>
        /// 判断账号在一年中是否变换过所有者
        /// </summary>
        /// <param name="year"></param>
        /// <param name="accountid"></param>
        /// <returns></returns>
        public bool HasChangedOwnerInYear(int year, int accountid)
        {
            using (DB.DBManager db = new DB.DBManager())
            {
                bool hasChanged = false;
                for (int i = 1; i < 13; i++)
                {
                    hasChanged = HasChangedOwnerInMonth(year, i, accountid);
                    if (hasChanged == true)
                    {
                        break;
                    }
                }
                db.Dispose();
                return hasChanged;
            }
        }
        /// <summary>
        /// 判断账号在一周中是否变换过所有者
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="week"></param>
        /// <param name="accountid"></param>
        /// <returns></returns>
        public bool HasChangedOwnerInWeek(int year, int month, int week, int accountid)
        {
            using (DB.DBManager db = new DB.DBManager())
            {
                bool hasChanged = false;
                Interface.Sys.ITradeHistory th = new Sys.ServiceTradeHistory(db);
                //Models.Sys.TradeHistory th = new Models.Sys.TradeHistory();
                List<int> operatorids = new List<int>();
                var query = db.DayOperatorAccounts.Where(a => a.AccountId == accountid).Where(a => a.Pdate.Year == year).Where(a => a.Pdate.Month == month).ToList();
                foreach (Models.Sys.DayOperatorAccount account in query)
                {
                    int _week = th.WeekOfYear(account.Pdate);
                    if (_week == week)
                    {
                        if (operatorids.Count == 0)
                        {
                            operatorids.Add(account.OperatorId);
                        }
                        else
                        {
                            var _query = operatorids.Where(a => a == account.OperatorId);
                            if (_query.Count() == 0)
                            {
                                hasChanged = true;
                                break;
                            }
                        }
                    }
                }
                db.Dispose();
                return hasChanged;
            }
        }
    }
}